package Object;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.Getter;
import lombok.ToString;
import java.util.ArrayList;
import java.util.List;

/**
 * @author jklu
 * 对象--->baidu返回的一页，包含一页中所有图片对象
 */

public class BaiduPic {
    //一页中所有的图片集合
    private List<Pic> picList = new ArrayList<Pic>();

    public List<Pic> getPicList() {
        return picList;
    }

    /**
     * 百度源地址解密
     * @param urlEncode 加密的源地址
     * @return          解密后的地址
     */
    private String decode(String urlEncode){
        StringBuilder stringBuilder = new StringBuilder();
        urlEncode = urlEncode.replace("ippr","http");
        urlEncode = urlEncode.replace("_z2C$q",":");
        urlEncode = urlEncode.replace("AzdH3F","/");
        urlEncode = urlEncode.replace("_z&e3B",".");
        urlEncode = urlEncode.toLowerCase();
        stringBuilder.append(urlEncode.substring(0,7));
        char[] temp = new char[urlEncode.length()-7];
        urlEncode.getChars(7,urlEncode.length(),temp,0);
        for (char a:temp) {
            switch(a){
                case 'w':
                    stringBuilder.append("a");
                    break;
                case 'k':
                    stringBuilder.append("b");
                    break;
                case 'v':
                    stringBuilder.append("c");
                    break;
                case '1':
                    stringBuilder.append("d");
                    break;
                case 'j':
                    stringBuilder.append("e");
                    break;
                case 'u':
                    stringBuilder.append("f");
                    break;
                case '2':
                    stringBuilder.append("g");
                    break;
                case 'i':
                    stringBuilder.append("h");
                    break;
                case 't':
                    stringBuilder.append("i");
                    break;
                case '3':
                    stringBuilder.append("j");
                    break;
                case 'h':
                    stringBuilder.append("k");
                    break;
                case 's':
                    stringBuilder.append("l");
                    break;
                case '4':
                    stringBuilder.append("m");
                    break;
                case 'g':
                    stringBuilder.append("n");
                    break;
                case '5':
                    stringBuilder.append("o");
                    break;
                case 'r':
                    stringBuilder.append("p");
                    break;
                case 'q':
                    stringBuilder.append("q");
                    break;
                case '6':
                    stringBuilder.append("r");
                    break;
                case 'f':
                    stringBuilder.append("s");
                    break;
                case 'p':
                    stringBuilder.append("t");
                    break;
                case '7':
                    stringBuilder.append("u");
                    break;
                case 'e':
                    stringBuilder.append("v");
                    break;
                case 'o':
                    stringBuilder.append("w");
                    break;
                case '8':
                    stringBuilder.append("1");
                    break;
                case 'd':
                    stringBuilder.append("2");
                    break;
                case 'n':
                    stringBuilder.append("3");
                    break;
                case '9':
                    stringBuilder.append("4");
                    break;
                case 'c':
                    stringBuilder.append("5");
                    break;
                case 'm':
                    stringBuilder.append("6");
                    break;
                case '0':
                    stringBuilder.append("7");
                    break;
                case 'b':
                    stringBuilder.append("8");
                    break;
                case 'l':
                    stringBuilder.append("9");
                    break;
                case 'a':
                    stringBuilder.append("0");
                    break;
                default:
                    stringBuilder.append(String.valueOf(a));
                    break;
            }
        }
        return stringBuilder.toString();
    }

    /**
     * 构造函数
     * @param json  调用baidu接口后，返回的原始信息，Baidu返回的为Json结构体数据
     * @param begin 当前页，图片的起始地址
     */
    public BaiduPic(String json, int begin){
        //解析Json
        JSONArray array = JSON.parseObject(json).getJSONArray("data");
        //图片的序号，起始地址+count
        int count = 1;
        for (int i=0;i<array.size()-1;i++){
            JSONObject temp = array.getJSONObject(i);
            //依次获取每张图片的信息
            int number = count+begin;
            String url = decode(temp.getString("objURL"));
            int width = temp.getIntValue("width");
            int height = temp.getIntValue("height");
            String type = temp.getString("type");
            //去除特殊符号
            String regex = "[`~!@#$%^&*()_\\-+=<>?:\"{}|,.\\\\/;']";
            //去除html标签
            String title = temp.getString("fromPageTitleEnc").replaceAll("<\\\\?/?\\w+>","").replaceAll(regex,"");
            if(title.isEmpty())
                title = String.valueOf(number)+String.valueOf(width)+String.valueOf(height);
            picList.add(new Pic(number,width,height,title,url,type));
            count++;
        }
    }

}
